package com.woniu.cloud.storeassembly.apiimpl;

import com.woniu.cloud.api.StoreServiceMiddleApi;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TStoreServiceMiddle;
import com.woniu.cloud.form.StoreServiceMiddleInsertForm;
import com.woniu.cloud.form.TStoreServiceUpdateMiddle;
import com.woniu.cloud.storeassembly.service.ITStoreServiceMiddleService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 平台对店铺商品表 前端控制器
 * </p>
 *
 * @author hyy
 * @since 2021-09-22
 */
@RestController

@Slf4j
public class StoreServiceMiddleApiImpl implements StoreServiceMiddleApi {



    @Resource
    private ITStoreServiceMiddleService storeServiceMiddleService;

    @Override
    @PostMapping("insertStoreServiceMiddle")
    @GlobalTransactional
    public Result insertStoreServiceMiddle(@RequestBody StoreServiceMiddleInsertForm storeServiceMiddleForm) {
        log.info("添加二级服务->storeServiceMiddleForm:{}",storeServiceMiddleForm);
        if (null==storeServiceMiddleForm) {
            return Result.fail("参数异常",null);
        }
        try {
            TStoreServiceMiddle serviceMiddle = new TStoreServiceMiddle();
            BeanUtils.copyProperties(storeServiceMiddleForm, serviceMiddle);
            log.info("开始添加->serviceMiddle:{}",serviceMiddle);
            return storeServiceMiddleService.InsertStoreServiceMiddle(serviceMiddle);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }

    }

    @Override
    @GlobalTransactional
    @GetMapping("getStoreServiceMiddleListByStoreId")
    public Result getStoreServiceMiddleListByStoreId(@RequestParam("storeId")Integer storeId) {
        log.info("通过店铺Id查找所有二级服务->storeId:{}",storeId);
        if (null==storeId) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始查询->storeId:{}",storeId);
            return storeServiceMiddleService.getStoreServiceMiddleListByStoreId(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @GlobalTransactional
    @PutMapping("updateStoreServiceMiddleStatusByStoreServiceMiddleId")
    public Result updateStoreServiceMiddleStatusByStoreServiceMiddleId(
            @RequestParam("storeServiceMiddleId")Integer storeServiceMiddleId,
            @RequestParam("storeServiceMiddleStatus")Integer storeServiceMiddleStatus) {
        log.info("通过Id下架二级服务->storeId:{},status:{}",storeServiceMiddleId,storeServiceMiddleStatus);
        if (null==storeServiceMiddleId) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始下架->storeServiceMiddleId:{}",storeServiceMiddleId);
            return storeServiceMiddleService.updateStoreServiceMiddleStatusByStoreServiceMiddleId(storeServiceMiddleId,storeServiceMiddleStatus);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @PutMapping("updateStoreServiceMiddleById")
    @GlobalTransactional
    public Result updateStoreServiceMiddleById(@RequestBody
            TStoreServiceUpdateMiddle storeServiceUpdateMiddle) {
        log.info("通过Id修改二级服务->storeServiceUpdateMiddle:{}",storeServiceUpdateMiddle);
        if (null==storeServiceUpdateMiddle) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始修改->storeServiceUpdateMiddle:{}",storeServiceUpdateMiddle);
            return storeServiceMiddleService.updateStoreServiceMiddleById(storeServiceUpdateMiddle);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }

    }

    @Override
    public Result getStoreServiceMiddleById(Integer id) {
        log.info("id:{}",id);
        TStoreServiceMiddle storeServiceMiddle = storeServiceMiddleService.getStoreServiceMiddleById(id);
        log.info("storeServiceMiddle:{}",storeServiceMiddle);
        return Result.success("查询成功",storeServiceMiddle);
    }
}
